<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "gky_qyh_sp_business".
 *
 * @property integer $id
 * @property integer $proposer_id
 * @property integer $approver_id
 * @property string $members
 * @property string $start_time
 * @property string $end_time
 * @property string $reason
 * @property string $route
 * @property string $transportation
 * @property string $eat_live
 * @property string $funding
 * @property string $others
 * @property string $agree_time
 * @property string $create_time
 * @property integer $status
 * @property string $print_code
 * @property string $car_num
 * @property string $driver
 */
class GkyQyhSpBusiness extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'gky_qyh_sp_business';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['proposer_id', 'approver_id', 'start_time', 'end_time', 'reason', 'route', 'transportation', 'eat_live', 'funding', 'create_time', 'status', 'print_code'], 'required'],
            [['proposer_id', 'approver_id', 'status'], 'integer'],
            [['start_time', 'end_time', 'agree_time', 'create_time'], 'safe'],
            [['members', 'reason', 'route', 'transportation', 'eat_live', 'funding', 'others', 'print_code', 'car_num', 'driver'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'proposer_id' => 'Proposer ID',
            'approver_id' => 'Approver ID',
            'members' => 'Members',
            'start_time' => 'Start Time',
            'end_time' => 'End Time',
            'reason' => 'Reason',
            'route' => 'Route',
            'transportation' => 'Transportation',
            'eat_live' => 'Eat Live',
            'funding' => 'Funding',
            'others' => 'Others',
            'agree_time' => 'Answer Time',
            'create_time' => 'Create Time',
            'status' => 'Status',
            'print_code' => 'Print Code',
            'car_num' => 'Car Num',
            'driver' => 'Driver',
        ];
    }
    
    public static function getBusiness($printCode){
        $result = (new \yii\db\Query())
                ->select('b.*,u.name approver,u.signature')
                ->from('gky_qyh_sp_business b')
                ->leftJoin('gky_qyh_user u','u.id=b.approver_id')
                ->where(['b.print_code'=>$printCode])
                ->one();
        $arr = array();
        $arr =@split(',', $result['members']);
        $num = count($arr);
        $temp =  (new \yii\db\Query)->select('u.name,u.position,d.name department')
                ->from('gky_qyh_user u')
                ->leftJoin('gky_qyh_dept_user du','du.user_id=u.userid')
                ->leftJoin('gky_qyh_department d','d.id=du.dept_id')
                ->where(['u.id'=> $result['proposer_id']])->one();
        $result['member'][0] = $temp;
        for($i=0; $i < $num; $i++){
            $user=  (new \yii\db\Query)->select('u.name,u.position,d.name department')
                ->from('gky_qyh_user u')
                ->leftJoin('gky_qyh_dept_user du','du.user_id=u.userid')
                ->leftJoin('gky_qyh_department d','d.id=du.dept_id')
                ->where(['u.id'=> $arr[$i]])->one();
            if($user!=null){
                $result['member'][$i+1] = $user;
            }
        }
        return  $result;
    }
    
    public static function agreeBusiness($printCode,$opinion){
        $model= GkyQyhSpBusiness::findOne(['print_code'=>$printCode]);
        $model->status=1;
        $model->opinion=$opinion;
        $model->agree_time=date('Y-m-d H:m:s');
        return $model->save();
    }
    public static function refuseBusiness($printCode,$opinion){
        $model= GkyQyhSpBusiness::findOne(['print_code'=>$printCode]);
        $model->status=-1;
        $model->opinion=$opinion;
        $model->agree_time=date('Y-m-d H:m:s');
        return $model->save();
    }
}
